package com.mail.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mail.common.DataResult;
import com.mail.dto.UserDto;
import com.mail.mapper.UserMapper;
import com.mail.pojo.User;
import com.mail.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    /***
     * 管理员可访问，返回登录用户名
     * @param authentication
     * @return
     */
    @GetMapping("/name")
    public DataResult name(OAuth2Authentication authentication) {
        User user=userMapper.queryUserIdByUserName(authentication.getName());
        return DataResult.success(user);
    }

    /**
     * 用户注册
     * 注册信息：username，password，nickname
     */
    @PostMapping(value = "/userRegister")
    public DataResult UserRegister(UserDto userDto){
        return userService.UserRegister(userDto);
    }

    /**
     * 修改用户相关信息
     * 可修改：username，password，nickname，img，status
     */
    @PutMapping("/updateUser")
    public DataResult update(@RequestParam Map<String, Object> map) {
        String id = (String) map.get("id");
        if (id.length() == 0) {
            return DataResult.error("请输入id！");
        }
        return userService.updateUser(map);
    }

    /**
     * 分页查询用户
     * 分页查询信息：page，limit
     */
    @GetMapping("/queryUserPage")
    public DataResult query(@RequestParam Map<String, Object> map) {
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        if (page.length() == 0 || limit.length() == 0) {
            return DataResult.error("请传入参数！");
        }
        if (Long.parseLong(page) < 0 || Long.parseLong(limit) < 0) {
            return DataResult.error("请传入正确的参数！");
        }
        return userService.queryUserPage(map);
    }

    /**
     * 根据id查询用户
     */
    @GetMapping("/getById/{id}")
    public DataResult getById(@PathVariable long id) {
        return userService.getUserById(id);
    }

    /**
     * 根据id删除用户
     * 删除：id
     */
    @DeleteMapping("/deleteUserById/{id}")
    public DataResult delete(@PathVariable long id) {
        return userService.deleteUserById(id);
    }

    /**
     * 找回密码
     */
    @PutMapping("/queryPasswordByName")
    public DataResult queryPasswordByName(@RequestParam("username") String username,@RequestParam("password") String password){
        System.out.println("aaa"+username);
        if(!StringUtils.isNotBlank(username) || !StringUtils.isNotBlank(password)){
            System.out.println(1);
            return DataResult.error("参数格式错误！");
        }else{
            return userService.queryPasswordByName(username,password);
        }
    }
}
